package com.db.concurrency.vehicles;

import java.util.*;

/**
 * MonitorVehicleTracker
 * <p/>
 * Monitor-based vehicle tracker implementation
 * 
 * @author Anton Kotov
 */

public class MonitorVehicleTracker {
  private final Map<String, MutablePoint> locations;

  public MonitorVehicleTracker(Map<String, MutablePoint> data) {
    locations = deepCopy(data);
  }

  public synchronized MutablePoint getLocation(String name) {
    return new MutablePoint(safeGet(name));
  }

  public synchronized void setLocation(String name, MutablePoint point) {
    MutablePoint loc = safeGet(name);
    loc.x = point.x;
    loc.y = point.y;
  }

  private MutablePoint safeGet(String name) {
    MutablePoint loc = locations.get(name);
    if (loc == null)
      throw new IllegalArgumentException("No such ID: " + name);
    return loc;
  }

  public synchronized Map<String, MutablePoint> getLocations() {
    return deepCopy(locations);
  }

  private Map<String, MutablePoint> deepCopy(Map<String, MutablePoint> data) {
    Map<String, MutablePoint> copy = new HashMap<String, MutablePoint>();
    for (String id : data.keySet()) {
      MutablePoint point = data.get(id);
      copy.put(id, new MutablePoint(point));
    }
    return copy;
  }

}
